<?php
/*
Coder: NetPuter.
Description: 种子团主程序. 从 XML 目录读取同一城市的所有 XML 文件 -> 读取内容并导出 RSS 文件 -> OK!
*/
include_once("func.php");

function translate_callback($match) {
	return translate($match[1]);
}

// 按城市名生成列表
$cityConfig = file(ABSPATH."list/citylist");
array_splice($cityConfig, 0, 1).array_splice($cityConfig, -1, 1);
$filter = array("'", ",");

for ($i=1;$i<count($cityConfig);$i=$i+2) {
	$cityFormat = str_replace($filter, "", $cityConfig[$i]);
	$citySplit = explode(":", trim($cityFormat));
	$config[] = $citySplit[0];
}

// 记录已支持的城市
$existCity = "";

// 读取对应城市的所有团
foreach ($config as $city) {
	// exec("ls xml/*_$city.xml > city/file_$city");
	$tuans = file_exists(ABSPATH."list/file_$city") ? file(ABSPATH."list/file_$city") : array(); // 仅 Win 下本地调试需要

	if (!count($tuans) == 0) {

		for ($i=0;$i<count($tuans);$i++) {
			$tuans[$i] = trim($tuans[$i]);
		}
		
		// 变量初始化
		$temp = $cat["all"] = $cat["eat"] = $cat["play"] = $cat["beauty"] = $cat["sports"] = $cat["person"] = $cat["other"] = "";

		// 读取对应团的所有团购信息
		foreach ($tuans as $tuan) {
			$deals = simplexml_load_file(ABSPATH."xml/$tuan");
			foreach ($deals->deal as $d) {
				$cSourceN = $d->source->name;
				$cSourceF = $d->source->full;
				$cSourceU = $d->source->url; // invite link
				$cName = $d->name;
				$cCats = strval($d->cats);
				$cVendorN = $d->vendor->name;
				$cVendorU = $d->vendor->url;
				$cStartT = gmdate("Y-m-d H:i:s", strtotime($d->start_time) + 3600*8);
				$cEndT = gmdate("Y-m-d H:i:s", strtotime($d->end_time) + 3600*8);
				$cPriceO = round($d->o_price);
				$cPriceC = round($d->c_price);
				$cDiscount = $d->discount;
				$cSaveM = round($d->save_money);
				$cImg = $d->img;
				$cUrl = $d->url;

				$currentCity = translate($city);
				$channel = translate($cCats);

				$vendorInfo = ($cVendorN == "") ? "" : "、<a target=\"_blank\" title=\"$cVendorN\" href=\"$cVendorU\">$cVendorN</a>";

				$temp = "
<div>
	<p style=\"font-size:20px;background:#f8f8f8;margin:15px 10px 0 0;padding-left:5px;border-left:5px solid #09C;border-bottom:1px solid #09C;\">
		<a target=\"_blank\" title=\"$cSourceF\" href=\"$cSourceU\"><img style=\"margin:auto 5px;\" alt=\"$cSourceF\" src=\"http://www.google.com/s2/favicons?domain_url=$cSourceU\" /></a><a target=\"_blank\" style=\"text-decoration:none;color:#09C;\" href=\"http://app.orzdream.com/tuan/$city/$cCats\" title=\"点击查看 种子团{$currentCity}站{$channel}频道 详情...\">「".translate($cCats)."」</a><strong><a target=\"_blank\" title=\"点击查看团购详情...\" style=\"text-decoration:none;color:#09C;\" href=\"$cUrl\">$cName</a></strong>
	</p>
	<blockquote style=\"font-size:16px;margin:15px 30px 0 12px;padding-left:20px;border-left:5px solid #DDD;\">
		<p>原价 $cPriceO 元，现价 <strong style=\"color:red\">$cPriceC</strong> 元，帮您省 <strong>$cSaveM</strong> 元，享受 $cDiscount 折优惠。</p>
		<p>团购时间： $cStartT 至 $cEndT</p>
		<p>活动地点： <a target=\"_blank\" title=\"$cSourceF\" href=\"$cSourceU\">$cSourceF</a>$vendorInfo</p>
		<p><a target=\"_blank\" title=\"$cName\" href=\"$cUrl\"><img alt=\"$cVendorN\" src=\"$cImg\" /></a></p>
	</blockquote>
</div>";

				if ($cCats == "all") {
					$cat["all"] .= $temp;
				} else {
					$cat["all"] .= $temp;
					$cat[$cCats] .= $temp;
				}

			}

			$done = substr($tuan, 0, strlen($tuan)-3)."done";;
			// exec("mv xml/$tuan xml/$done");
		}
		
		// 将内容输出为 RSS
		$pubTime = substr(date("l"),0,3).", ".gmdate("d M Y H:i:s O", time());
		$today = gmdate("Y-m-d", mktime() + 8 * 3600);
		$weather = "<div style=\"font-size:14px;background:#f8f8f8;border:1px solid #09C;padding:5px;\">".weather($city)."</div>";
		
		foreach($cat as $k=>$v) {
			if ($v !== "") {
				$currentCity = translate($city);
				$channel = translate($k);
				$more = "<hr />想订阅其它城市的每日团购聚合吗？<a href=\"http://app.orzdream.com/tuan/\" title=\"种子团 FeedTuan\">到「种子团」看看吧</a>！</p>";

				$rss = "<rss version=\"2.0\"
	xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"
	xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"
	xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
	xmlns:atom=\"http://www.w3.org/2005/Atom\"
	xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"
	xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"
	>

	<channel>
		<title>种子团 {$currentCity}站 {$channel}频道</title>
		<atom:link href=\"http://app.orzdream.com/tuan/feed/$city\" rel=\"self\" type=\"application/rss+xml\" />
		<link>http://app.orzdream.com/tuan/$city</link>
		<description>待定</description>
		<lastBuildDate>$pubTime</lastBuildDate>
		<language>zh-CN</language>
		<generator>http://app.orzdream.com/tuan/</generator>
		<item>
			<title>$currentCity 每日团购聚合 ($today)</title>
			<link>http://app.orzdream.com/tuan/$city</link>
			<pubDate>$pubTime</pubDate>
			<description><![CDATA[$weather $v $more]]></description>
			<guid>http://app.orzdream.com/tuan/$city</guid>
		</item>
	</channel>
</rss>";

				$fc = fopen(ABSPATH."feed/{$city}_{$k}.rss", "wb");
				fwrite($fc, $rss);
				fclose($fc);

			}
		}
		$existCity .= "$city\n";
	}
	
	// exec("mv city/$city city/$city.done");
}

$ec = fopen(ABSPATH."list/exist", "wb");
fwrite($ec, $existCity);
fclose($ec);

echo "Done!";
?>